package com.software.project.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.log.Log;
import com.google.gson.Gson;
import com.software.project.domain.Result;
import com.software.project.entity.Suit;
import com.software.project.entity.UserInfo;
import com.software.project.service.SuitService;
import com.software.project.service.UserInfoService;
import com.software.project.utils.LogUtil;
import com.software.project.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/suit")
@Slf4j
public class SuitController {
    @Autowired
    private SuitService suitService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private HttpServletRequest request;
    private String[] numbers={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"};

    //根据服装id查找服装的方法
    @GetMapping("/findSuitBySuitId")
    public Suit findSuitBySuitId(int suitId) {
        Suit suit = suitService.findSuitBySuitId(suitId);
        if (suit != null) {
            log.info(LogUtil.success(request.getRemoteAddr(), suitId + ""));
        } else {
            // 非法查询
            log.info(LogUtil.error(request.getRemoteAddr(), suitId + ""));
        }
        return suit;
    }
    //根据服装图片路径查找服装的方法
    @GetMapping("/findSuitBySuitImage")
    public Suit findSuitBySuitImage(String wearSuit){
        Suit suit = suitService.findSuitBySuitImage(wearSuit);
        if (suit != null) {
            log.info(LogUtil.success(request.getRemoteAddr(), wearSuit));
        } else {
            // 非法查询
            log.info(LogUtil.error(request.getRemoteAddr(), wearSuit));
        }
        return suit;
    }

    @GetMapping("/findAllSuit")
    public Result findAllSuit(String userId){
        //通过SuitController的三个方法获取已拥有的服装list 可解锁的服装list 不可解锁的服装list
        List<Suit> unlockedSuitList=findUnlockedSuit(userId);
        List<Suit> unlockingSuitList=findUnlockingSuit(userId);
        List<Suit> lockedSuitList=findLockedSuit(userId);
        //创建Gson对象 用于将List转换为json数据 传回前端
        Gson suitGson = new Gson();
        //将三个list转换为json数组
        JSONArray unlockedSuitArray=new JSONArray(suitGson.toJson(unlockedSuitList));
        JSONArray unlockingSuitArray=new JSONArray(suitGson.toJson(unlockingSuitList));
        JSONArray lockedSuitArray=new JSONArray(suitGson.toJson(lockedSuitList));
        //创建一个存放三种状态的服装数组的JSONObject
        JSONObject allSuit=new JSONObject();
        //将三种状态的服装数组存进allSuit
        allSuit.put("unlocked",unlockedSuitArray);
        allSuit.put("unlocking",unlockingSuitArray);
        allSuit.put("locked",lockedSuitArray);
        log.info(LogUtil.success(request.getRemoteAddr(), userId));
        return ResultUtil.success("200","返回武器数据成功",allSuit);
    }
    //查找用户已拥有的服装
    @GetMapping("/findUnlockedSuit")
    public List<Suit> findUnlockedSuit(String userId){
        //根据用户id拿到用户对象
        UserInfo userInfo=userInfoService.findUserInfoByUserId(userId);
        //拿到个人服装库
        String suitStore=userInfo.getSuitStore();
        JSONObject jsonObject=new JSONObject(suitStore);
        List<Suit> list=new ArrayList<>();
        //把个人服装库的json的每个key对应的值找出来 这就是服装id  用服装id找到服装 存入list
        for (String key:jsonObject.keySet()) {
            Integer integer=jsonObject.getInt(key);
            Suit suit=suitService.findSuitBySuitId(integer);
            list.add(suit);
        }
        log.info(LogUtil.success(request.getRemoteAddr(), userId));
        return list;
    }
    //查找可解锁的服装
    @GetMapping("/findUnlockingSuit")
    public List<Suit> findUnlockingSuit(String userId){
        //根据用户id拿到用户对象
        UserInfo userInfo=userInfoService.findUserInfoByUserId(userId);
        //拿到个人服装库
        String suitStore=userInfo.getSuitStore();
        //传入类型进行强转 这里拿到的是已拥有和可解锁混合的服装list
        List<Suit> scopeSuit=suitService.findUnlockingSuit((int)userInfo.getIntelligence(),(int)userInfo.getPower());
        JSONObject jsonObject=new JSONObject(suitStore);
        //把个人服装库的json的每个key对应的值找出来 这就是服装id  从混合服装list里删除掉这些服装id对应的服装对象
        for (String key:jsonObject.keySet()) {
            Integer integer=jsonObject.getInt(key);
            Suit suit=suitService.findSuitBySuitId(integer);
            scopeSuit.remove(suit);
        }
        log.info(LogUtil.success(request.getRemoteAddr(), userId));
        return scopeSuit;
    }
    //查找不可解锁的服装
    @GetMapping("/findLockedSuit")
    public List<Suit> findLockedSuit(String userId){
        //根据用户id拿到用户对象
        UserInfo userInfo=userInfoService.findUserInfoByUserId(userId);
        //传入类型进行强转
        List<Suit> scopeSuit=suitService.findLockedSuit((int)userInfo.getIntelligence(),(int)userInfo.getPower());
        log.info(LogUtil.success(request.getRemoteAddr(), userId));
        return scopeSuit;
    }

    //点击解锁按钮 默认这里是系统帮我们筛选出来了 所以不用判断了
    @GetMapping("/unlockSuit")
    public Result unlockSuit(String userId,Integer suitId){
        //根据用户id获取用户对象
        UserInfo userInfo=userInfoService.findUserInfoByUserId(userId);
        //获取用户服装库
        String suitStore=userInfo.getSuitStore();
        //将服装库转成json对象
        JSONObject jsonObject=new JSONObject(suitStore);
        //把转过来的服装id存入json对象
        jsonObject.put(numbers[suitId-1],suitId);
        //设置新的服装库
        userInfo.setSuitStore(jsonObject.toString());
        //更新服装库
        UserInfo userInfoTemp=userInfoService.createUserInfo(userInfo);
        log.info(LogUtil.success(request.getRemoteAddr(), userId + " " + suitId));
        return ResultUtil.success("200","解锁成功",null);
    }

    //点击穿戴按钮
    @GetMapping("/wearSuit")
    public Result wearSuit(String userId, Integer suitId){
        //根据用户id获取用户对象
        UserInfo userInfo = userInfoService.findUserInfoByUserId(userId);
        //根据服装id获取新服装对象
        Suit suit=findSuitBySuitId(suitId);
        if(userInfo.getWearSuit().equals(suit.getSuitSrc())){
            return ResultUtil.success("404","您已经穿戴上这件服装了",null);
        }
        //根据用户穿戴服装的图片路径获取旧武器对象
        Suit oldSuit=findSuitBySuitImage(userInfo.getWearSuit());
        //重新设置用户魅力值
        userInfo.setCharm(userInfo.getCharm()-oldSuit.getSuitCharm()+suit.getSuitCharm());
        userInfo.setComprehensive(userInfo.getIntelligence()*0.5+userInfo.getPower()*0.3+userInfo.getCharm()*0.2);
        //给当前对象更改穿戴服装的图片
        userInfo.setWearSuit(suit.getSuitSrc());
        //更新用户穿戴服装的图片和魅力值和综合素质
        UserInfo userInfoTemp=userInfoService.createUserInfo(userInfo);
        log.info(LogUtil.success(request.getRemoteAddr(), userId + " " + suitId));
        return ResultUtil.success("200","穿戴成功",null);
    }
}
